import Session from '../service/Session';
import Api from '../service/Api';

class UserManager {
    constructor() {
        this.userInfo = Session.getUser();
        this.users = {};
        this.requests = {};
    }

    queryUser(id) {
        let deferred = $.Deferred();

        if (this.users[id]) {
            deferred.resolve(this.users[id]);
        } else if (this.requests[id]) {
            this.requests[id].then((u) => {
                deferred.resolve(u);
            });
        } else {
            this.requests[id] = Api.get('/BasePlate/Interface/IInterfaceJson.asmx/UserInfo_Get', {
                GetUser_ID: id,
                SessionID: this.userInfo.SessionID
            }).then(resp => {
            	console.log(resp)
            	console.log('请求数据')
            	console.log(id)
            	console.log(this.userInfo.SessionID)
                this.users[id] = resp.Goodo ? resp.Goodo : undefined;
                this.requests[id] = undefined;

                deferred.resolve(this.users[id]);

                return resp.Goodo;
            });
        }

        return deferred;
    }
}

export default new UserManager();
